package startup;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

/** * @author salever * */
public class TableViewerDemo extends Dialog {
	List<String> arrayList;
	TableViewer viewer;

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return null;
		}
	}

	class TableContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			if (parent instanceof List) {
				return ((List) parent).toArray();
			}
			return new String[] { "One", "Two", "Three" };
		}
	}

	/** * */
	public TableViewerDemo(Shell shell) {
		super(shell);
	} /*
	 *  * (non-Javadoc) * * @see *
	 * org.eclipse.jface.dialogs.Dialog#createContents(org.eclipse.swt.widgets *
	 * .Composite)
	 */

	@Override
	protected Control createContents(Composite parent) {
		Composite container = parent;
		Composite com = new Composite(container, SWT.BORDER);
		container.setLayout(new FillLayout());
		com.setLayout(new FillLayout());
		Button button = new Button(com, SWT.NONE);
		button.setText("Delete");
		viewer = new TableViewer(com, SWT.MULTI | SWT.BORDER);
		viewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setContentProvider(new TableContentProvider());
		arrayList = new ArrayList<String>();
		arrayList.add("One");
		arrayList.add("Two");
		arrayList.add("Three");
		viewer.setInput(arrayList);
		button.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				arrayList.remove(0);
				viewer.refresh();
			}
		});
		return container;
	} /*
	 *  * (non-Javadoc) * * @see *
	 * org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets *
	 * .Shell)
	 */

	@Override
	protected void configureShell(Shell newShell) {
		newShell.setText("Table viewer");
		newShell.setSize(480, 640);
		super.configureShell(newShell);
	}

	/** * @param args */
	public static void main(String[] args) {
		TableViewerDemo dlg = new TableViewerDemo(new Shell());
		dlg.open();
	}
}
